<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'core/app/AppAjax.php';


class KBCategoryView_form extends AppView
{
	var $tmpl = 'form.html';
	
	
	function execute(&$obj, &$manager) {
		
		$this->addMsg('user_msg.ini');
		
		$tpl = &new tplTemplatez($this->template_dir . $this->tmpl);
		$tpl->tplAssign('error_msg', AppMsg::errorBox($obj->errors));
		
		
		$rows = &$manager->getSelectRecords();
		$range = &$manager->getSelectRange($rows);
		$js_values =& $manager->getSortJsArray($rows);
		
		foreach($js_values as $k => $v) {
			$a['js_option_list_values'] = $v;
			$key = ($obj->get('id')) ? $obj->get('sort_order') : 'sort_end';
			$a['js_option_default'] = sprintf("'%s', '%s'", $k, $key);
			$tpl->tplParse($a, 'js_option_list');
		}		
		
		// category
		$select = new FormSelect();
		$select->setSelectWidth(250);
		$select->select_tag = false;
		
		// set disabled for self
		if($obj->get('id')) { 
			$select->setOptionParam($obj->get('id'), 'disabled');
		}
		
		foreach($rows as $k => $v) {
			if($v['private']) {
				$select->setOptionParam($k, 'style="color: #cc0033;"');
			}
		}
		
		$select->setMultiple(5, false);
		$select->setSelectName('parent_id');
		$select->setRange($range, array(0=>$this->msg['top_level_msg']));
		$tpl->tplAssign('category_select', $select->select($obj->get('parent_id')));
		
		
		// admin users
		if($manager->isExtra('notify_admin_category')) {
			
			$tpl->tplSetNeededGlobal('admin_user');
			
			foreach($obj->getAdminUser() as $id => $name) {
				$data = array('user_id'=>$id, 'name'=>$name);
				$data['delete_msg'] = $this->msg['delete_msg'];
				$tpl->tplParse($data, 'admin_user_row');
			}		
		}
		
		
		// member roles
		if($manager->isExtra('private')) {
		
			$select->resetOptionDisabled();
			$select->setSelectName('member_role');
			$select->setRange($manager->getMemberRoleSelectRange());
			$tpl->tplAssign('member_role_select', $select->select($obj->getMemberRole()));
			
			//xajax
			$xajax = &new AppAjax();
			//$xajax->debugOn();
			$xajax->registerFunction(array('addParentRoles', $this, 'ajaxAddParentRoles'));
			$xajax_js = $xajax->getJavascript();
			$xajax->processRequests();
			
			$tpl->tplSetNeededGlobal('private');
			$tpl->tplAssign('xajax_js', $xajax_js);
		}	
		
		
		$tpl->tplAssign('private_options', $this->getChecked($obj->get('private')));
		$tpl->tplAssign('commentable_options', $this->getChecked($obj->get('commentable')));
		$tpl->tplAssign('ratingable_options', $this->getChecked($obj->get('ratingable')));	
				
		$tpl->tplAssign($this->setCommonFormVars($obj));
		$tpl->tplAssign($this->setStatusFormVars($obj->get('active')));		
		$tpl->tplAssign($obj->get());
		$tpl->tplAssign($this->msg);
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function ajaxAddParentRoles($category_id) {
		
		if($category_id == 0) { 
			$objResponse = new xajaxResponse();
			return $objResponse;
		}
		
		
		$manager = &new KBCategoryModel();
		
		$roles_ids = false;
		$is_private = $manager->isPrivate($category_id);
		if($is_private) {
			$roles_ids = implode(',', $manager->getMemberRoleById($category_id));
		}
		
		$objResponse = new xajaxResponse();
		
		//$objResponse->addAlert($category_id);
		//$objResponse->addAlert($is_private);
		//$objResponse->addAlert($roles_ids);
		
		$objResponse->addScriptCall('AssignPrivateFromParent', $is_private, $roles_ids);
	
		return $objResponse;	
	}
}	
?>
